<script type="text/javascript" src="<?php echo URL_JS; ?>ckeditor/ckeditor.js"></script>
<script type="text/javascript" src="<?php echo URL_JS; ?>update_news_form.js"></script>
<script type="text/javascript">

</script><?php
if (!defined('CONFIGURATION_FILE_LOADED'))
    include( 'includes/configuration_include.php');
include( 'includes/regions.php');
include( 'includes/id_regions.php');


include( PATH_VIEWS . 'update_work_view.php');
include( PATH_MODELS . 'update_work_model.php');

$bdd = model_db_connexion();
$count_works = count_works($bdd);

// Si l'utilisateur est deja logue
if (!empty($_SESSION['id_user'])) {


    echo'<span class="template_title">Gestion des oeuvres</span>';
    if (isset($_POST['delete'])) {
        echo 1;
        $work_id = $_POST['delete'];
        delete_work($bdd, $work_id);
    }
    if (isset($_POST['work_id'])) {
        
        $work_id = $_POST['work_id'];

        $user_name = $_POST['user_name' . $work_id . ''];
        $work_name = $_POST['work_name' . $work_id . ''];
        $region_form = $_POST['id_region' . $work_id . ''];
        $req4 = select_id_user($bdd,$user_name);
        while($donnees = $req4->fetch(PDO::FETCH_ASSOC))
        {
            $user_name = $donnees['id_user'];
        }
        
        $region = id_region($region_form);
        $work_description = $_POST['description' . $work_id . ''];


        if (isset($_FILES['photo' . $work_id . '']) AND $_FILES['photo' . $work_id . '']['error'] == 0) {
            if (upload_photo_test($work_id) != 3) {
               
                $type_img = upload_photo_test($work_id);
                upload_photo($work_id, $type_img);

                model_update_works($bdd, $work_description, $work_id, $type_img, $work_name,  $region, $user_name);
            } else {
                echo' Votre image  1 n\'est pas au bon format';
                model_update_works($bdd, $work_description, $work_id, 3, $work_name, $region, $user_name);
            }
        } else {
            model_update_works($bdd, $work_description, $work_id, 3, $work_name, $region, $user_name);
            
        }

        if (isset($_FILES['photob' . $work_id . '']) AND $_FILES['photob' . $work_id . '']['error'] == 0) {
            

            if (upload_photo_test2($work_id) != 3) {
                $type_img = upload_photo_test2($work_id);
                upload_photo2($work_id, $type_img);
                
                model_update_works2($bdd, $work_description, $work_id, $type_img, $work_name, $region, $user_name);
            } else {
                echo' Votre image 2 n\'est pas au bon format';
                model_update_works2($bdd, $work_description, $work_id, 2, $work_name, $region, $user_name);
            }
        } else {
            model_update_works2($bdd, $work_description, $work_id, 2, $work_name, $region, $user_name);


            model_update_works2($bdd, $work_description, $work_id, 2, $work_name, $region, $user_name);
            ;
        }
    }


    if (isset($_POST['envoyer'])) {
        if ($_POST['envoyer'] == 'Valider') {
            $req = model_works($bdd);

            while ($donnees = $req->fetch(PDO::FETCH_ASSOC)) {
                $i = $donnees['id_work'];
                $user_name = $_POST['user_name' . $i . ''];
                $work_name = $_POST['work_name' . $i . ''];
                $region_form = $_POST['id_region' . $i . ''];
                $region = id_region($region_form);
               $req4 = select_id_user($bdd,$user_name);
           while($donnees = $req4->fetch(PDO::FETCH_ASSOC))
          {
            $user_name = $donnees['id_user'];
           }
        
                $work_description = $_POST['description' . $i . ''];

                if (isset($_FILES['photob' . $i . '']) AND $_FILES['photob' . $i . '']['error'] == 0) {
                    if (upload_photo_test2($i) != 3) {
                        $type_img = upload_photo_test2($i);
                        upload_photo2($i, $type_img);
                        model_update_Allworks2($bdd, $work_description, $i, $type_img, $work_name, $region, $user_name);
                    } else {
                        echo' Une photo de l\'oeuvre numeros ' . $i . ' n\'est pas au bon format';
                        model_update_Allworks2($bdd, $work_description, $i, 3, $work_name, $region, $user_name);
                    }
                } else {

                    model_update_Allworks2($bdd, $work_description, $i, 3, $work_name, $region, $user_name);
                }
                if (isset($_FILES['photo' . $i . '']) AND $_FILES['photo' . $i . '']['error'] == 0) {
                    if (upload_photo_test($i) != 3) {
                        $type_img = upload_photo_test($i);
                        upload_photo($i, $type_img);
                        model_update_Allworks($bdd, $work_description, $i, $type_img, $work_name, $region, $user_name);
                    } else {
                        echo' Une photo de l\'oeuvre numeros ' . $i . ' n\'est pas au bon format';
                        model_update_Allworks($bdd, $work_description, $i, 3, $work_name, $region, $user_name);
                    }
                } else {

                    model_update_Allworks($bdd, $work_description, $i, 3, $work_name,  $region, $user_name);
                }
            }
        }
    }

    $req = model_works($bdd);
    start_form();
    while ($donnees = $req->fetch(PDO::FETCH_ASSOC)) {

        $work_description = $donnees['work_description'];
        $user_name = $donnees['user_name'];
        $work_id = $donnees['id_work'];
        $type_img = $donnees['img_type'];
        $work_name = $donnees['work_name'];
        $region = $donnees['id_region'];
       ?>
<script type="text/javascript">
    function visu($work_id)
    { var w = $work_id;
        CKEDITOR.replace("visu"+w, 
        {
            height:"300", width:"840"
        });}

    function envoi_form($work_id){
        var w = $work_id;
        document.form2.action = "<?php echo URL_ROOT . 'update-work/work_id-' ?>"+w;  


        document.form2.submit();
    } 
    function envoi_form_delete($work_id){
        var w = $work_id;
        document.form2.action = "<?php echo URL_ROOT . 'update-work/delete-' ?>"+w;  


        document.form2.submit();
    } 
</script>
        <?php
        $req2 = select_user($bdd);

        $str = printNews($work_description, $work_id, $type_img, $work_name, $region, $user_name, $req2);
//end_button();
        echo ' <script type="text/javascript"> visu(' . $work_id . ' );</script>';
    }

    end_form();
    } else {
    echo '<span class="template_span">' . LANG_NO_CONNECT . '</span>';
}
    ?>


    <?php

    function upload_photo($work_id, $type_img) {

        switch ($type_img) {
            case 'png' :
                if (file_exists('design/img/works/' . $work_id . '.png') == true) {
                    unlink('design/img/works/' . $work_id . '.png');
                }
                break;
            case 'jpg' :
                if (file_exists('design/img/works/' . $work_id . '.jpg') == true) {
                    unlink('design/img/works/' . $work_id . '.jpg');
                }

                break;
            case 'jpeg' :
                if (file_exists('design/img/works/' . $work_id . '.jpeg') == true) {
                    unlink('design/img/works/' . $work_id . '.jpeg')
                    ;
                }
                break;
        }

        if ($_FILES['photo' . $work_id . '']['size'] <= 7000000) {

            $infosfichier = pathinfo($_FILES['photo' . $work_id . '']['name']);

            $extension_upload = $infosfichier['extension'];
  $extension_upload =strtolower($extension_upload);

            $extensions_autorisees = array('jpeg', 'png', 'jpg');

            if (in_array($extension_upload, $extensions_autorisees)) {





                move_uploaded_file($_FILES['photo' . $work_id . '']['tmp_name'], "design/img/works/" . $work_id . "." . $extension_upload . "");
               fctredimimage(380,380,"design/img/works/"," . $work_id . "." . $extension_upload . ","design/img/works/",$work_id . "." . $extension_upload);

            } else {
                return false;
            }
        } else {
            echo 'Votre photo est trop lourde';
        }
    }

    function upload_photo_test($i) {



        $infosfichier = pathinfo($_FILES['photo' . $i . '']['name']);

        $extension_upload = $infosfichier['extension'];
  $extension_upload =strtolower($extension_upload);

        $extensions_autorisees = array('jpg', 'jpeg', 'png');

        if (in_array($extension_upload, $extensions_autorisees)) {




            switch ($extension_upload) {
                case 'png' :
                    return 0;
                    break;
                case 'jpg' :
                    return 1;
                    break;
                case 'jpeg' :
                    return 2;
                    break;
            }
            //$name= $id_work .".". $extension_upload;
        } else {
            return 3;
        }
        
        
        
    }

    function upload_photo2($work_id, $type_img) {



        switch ($type_img) {
            case 'png' :
                if (file_exists('design/img/works2/' . $work_id . '.png') == true) {
                    unlink('design/img/works2/' . $work_id . '.png');
                }
                break;
            case 'jpg' :
                if (file_exists('design/img/works2/' . $work_id . '.jpg') == true) {
                    unlink('design/img/works2/' . $work_id . '.jpg');
                }

                break;
            case 'jpeg' :
                if (file_exists('design/img/works2/' . $work_id . '.jpeg') == true) {
                    unlink('design/img/works2/' . $work_id . '.jpeg')
                    ;
                }
                break;
        }



        if ($_FILES['photob' . $work_id . '']['size'] <= 7000000) {


            $infosfichier = pathinfo($_FILES['photob' . $work_id . '']['name']);

            $extension_upload = $infosfichier['extension'];

            $extensions_autorisees = array('jpeg', 'png', 'jpg');

            if (in_array($extension_upload, $extensions_autorisees)) {



                //$name= $id_work .".". $extension_upload;

                move_uploaded_file($_FILES['photob' . $work_id . '']['tmp_name'], "design/img/works2/" . $work_id . "." . $extension_upload . "");
            } else {
                return false;
            }
        } else {
            echo 'Votre photo est trop lourde';
        }
    }

    function upload_photo_test2($i) {




        $infosfichier = pathinfo($_FILES['photob' . $i . '']['name']);

        $extension_upload = $infosfichier['extension'];

        $extensions_autorisees = array('jpg', 'jpeg', 'png');

        if (in_array($extension_upload, $extensions_autorisees)) {

            switch ($extension_upload) {
                case 'png' :
                    return 0;
                    break;
                case 'jpg' :
                    return 1;
                    break;
                case 'jpeg' :
                    return 2;
                    break;
            }

            //$name= $id_work .".". $extension_upload;
        } else {
            return 3;
        }
    }

 function fctredimimage($W_max, $H_max, $rep_Dst, $img_Dst, $rep_Src, $img_Src) {
 $condition = 0;
 // Si certains paramètres ont pour valeur '' :
 if ($rep_Dst=='') { $rep_Dst = $rep_Src; } // (même répertoire)
 if ($img_Dst=='') { $img_Dst = $img_Src; } // (même nom)
 // ---------------------
 // si le fichier existe dans le répertoire, on continue...
 if (file_exists($rep_Src.$img_Src) && ($W_max!=0 || $H_max!=0)) { 
   // ----------------------
   // extensions acceptées : 
	$extension_Allowed = 'jpg,jpeg,png';	// (sans espaces)
   // extension fichier Source
	$extension_Src = strtolower(pathinfo($img_Src,PATHINFO_EXTENSION));
   // ----------------------
   // extension OK ? on continue ...
   if(in_array($extension_Src, explode(',', $extension_Allowed))) {
     // ------------------------
      // récupération des dimensions de l'image Src
      $img_size = getimagesize($rep_Src.$img_Src);
      $W_Src = $img_size[0]; // largeur
      $H_Src = $img_size[1]; // hauteur
      // ------------------------
      // condition de redimensionnement et dimensions de l'image finale
      // ------------------------
      // A- LARGEUR ET HAUTEUR maxi fixes
      if ($W_max!=0 && $H_max!=0) {
         $ratiox = $W_Src / $W_max; // ratio en largeur
         $ratioy = $H_Src / $H_max; // ratio en hauteur
         $ratio = max($ratiox,$ratioy); // le plus grand
         $W = $W_Src/$ratio;
         $H = $H_Src/$ratio;   
         $condition = ($W_Src>$W) || ($W_Src>$H); // 1 si vrai (true)
      }
      // ------------------------
      // B- HAUTEUR maxi fixe
      if ($W_max==0 && $H_max!=0) {
         $H = $H_max;
         $W = $H * ($W_Src / $H_Src);
         $condition = ($H_Src > $H_max); // 1 si vrai (true)
      }
      // ------------------------
      // C- LARGEUR maxi fixe
      if ($W_max!=0 && $H_max==0) {
         $W = $W_max;
         $H = $W * ($H_Src / $W_Src);         
         $condition = ($W_Src > $W_max); // 1 si vrai (true)
      }
      // ---------------------------------------------
      // REDIMENSIONNEMENT si la condition est vraie
      // ---------------------------------------------
      // - Si l'image Source est plus petite que les dimensions indiquées :
      // Par defaut : PAS de redimensionnement.
      // - Mais on peut "forcer" le redimensionnement en ajoutant ici :
      // $condition = 1; (risque de perte de qualité)
      if ($condition==1) {
         // ---------------------
         // creation de la ressource-image "Src" en fonction de l extension
         switch($extension_Src) {
         case 'jpg':
         case 'jpeg':
           $Ress_Src = imagecreatefromjpeg($rep_Src.$img_Src);
           break;
         case 'png':
           $Ress_Src = imagecreatefrompng($rep_Src.$img_Src);
           break;
         }
         // ---------------------
         // creation d une ressource-image "Dst" aux dimensions finales
         // fond noir (par defaut)
         switch($extension_Src) {
         case 'jpg':
         case 'jpeg':
           $Ress_Dst = imagecreatetruecolor($W,$H);
           break;
         case 'png':
           $Ress_Dst = imagecreatetruecolor($W,$H);
           // fond transparent (pour les png avec transparence)
           imagesavealpha($Ress_Dst, true);
           $trans_color = imagecolorallocatealpha($Ress_Dst, 0, 0, 0, 127);
           imagefill($Ress_Dst, 0, 0, $trans_color);
           break;
         }
         // ---------------------
         // REDIMENSIONNEMENT (copie, redimensionne, re-echantillonne)
         imagecopyresampled($Ress_Dst, $Ress_Src, 0, 0, 0, 0, $W, $H, $W_Src, $H_Src); 
         // ---------------------
         // ENREGISTREMENT dans le repertoire (avec la fonction appropriee)
         switch ($extension_Src) { 
         case 'jpg':
         case 'jpeg':
           imagejpeg ($Ress_Dst, $rep_Dst.$img_Dst);
           break;
         case 'png':
           imagepng ($Ress_Dst, $rep_Dst.$img_Dst);
           break;
         }
         // ------------------------
         // liberation des ressources-image
         imagedestroy ($Ress_Src);
         imagedestroy ($Ress_Dst);
      }
      // ------------------------
   }
 }
 // ---------------------------------------------------
 // retourne : true si le redimensionnement et l'enregistrement ont bien eu lieu, sinon false
 if ($condition==1 && file_exists($rep_Dst.$img_Dst)) { return true; }
 else { return false; }
 // ---------------------------------------------------
};
?>
